package cn.itsource.controller;

import cn.itsource.annotation.PreAuthorize;
import cn.itsource.domain.Article;
import cn.itsource.domain.Param;
import cn.itsource.query.ArticleQuery;
import cn.itsource.query.ParamQuery;
import cn.itsource.service.IParamService;
import cn.itsource.util.R;
import cn.itsource.vo.ArticleCategory;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/param")
@Api(tags = "用户接口")
public class ParamController {
    @Autowired
    private IParamService paramService;

    /**
     * 增加/修改
     * @param param
     * @return
     */
    @ApiOperation(value = "增加/修改")
    @PutMapping
    @PreAuthorize(sn="param:add", name = "用户增加/修改",descs = "用户增加/修改")
    public R addORupdate(@RequestBody Param param){
        try {
            if (param.getId() == null){
                paramService.add(param);
            }else {
                paramService.update(param);
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(sn="param:del", name = "用户删除",descs = "用户删除")
    public R del(@PathVariable("id") Long id){
        try {
            paramService.del(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(sn="param:delList", name = "用户批量删除",descs = "用户批量删除")
    public R delList(@RequestBody List<Long> ids){
        try {
            paramService.delList(ids);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
    /**
     * 获取单个
     * @param id
     * @return
     */
    @ApiOperation(value = "获取单个")
    @GetMapping("/{id}")
    @PreAuthorize(sn="param:getOne", name = "用户获取单个",descs = "用户获取单个")
    public R getOne(@PathVariable("id") Long id){
        try {
            return R.ok(paramService.get(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    @PreAuthorize(sn="param:getAll", name = "用户查询所有",descs = "用户查询所有")
    public R getAll(){
        try {
            return R.ok(paramService.getAll());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(sn="param:pageQuery", name = "文章分页查询",descs = "文章分页查询")
    public PageInfo<Param> pageQuery(@RequestBody ParamQuery query){
        return paramService.pageQuery(query);
    }

}
